#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from ParseXml import ParseXml
import os
import sys

root_path = os.path.dirname(os.path.abspath(__file__))


def diff_xml(xml_file_01, xml_file_02):
    diff_list_01, diff_list_02 = [], []

    xml_doc_01 = ParseXml(xml_file_01)
    element_tree_01 = xml_doc_01.root
    element_list_01 = ParseXml.get_element_list(element_tree_01)
    element_list_01 = ParseXml.refactor_element(element_list_01)

    xml_doc_02 = ParseXml(xml_file_02)
    element_tree_02 = xml_doc_02.root
    element_list_02 = ParseXml.get_element_list(element_tree_02)
    element_list_02 = ParseXml.refactor_element(element_list_02)

    for idx, element in enumerate(element_list_01):
        if element not in element_list_02:
            diff_list_01.append(element)
            print('Xml文档一拥有差异元素{0}，位置{1}'.format(element, idx + 1))
            print('')

    for idx, element in enumerate(element_list_02):
        if element not in element_list_01:
            diff_list_02.append(element)
            print('Xml文档二拥有差异元素{0}，位置{1}'.format(element, idx + 1))
            print('')

    if len(diff_list_01) == 0:
        print('Xml文档一没有差异元素')

    if len(diff_list_02) == 0:
        print('Xml文档二没有差异元素')

file_exist_01, file_exist_02 = False, False
xml_path_01, xml_path_02 = None, None
for root, dirs, files in os.walk(root_path):
    for file in files:
        if '_01.xml' in file:
            file_exist_01 = True
            xml_path_01 = os.path.join(root_path, file)
        elif '_02.xml' in file:
            file_exist_02 = True
            xml_path_02 = os.path.join(root_path, file)

if file_exist_01 is True and file_exist_02 is False:
    print('xxx_02.xml 文档不存在')
    sys.exit(1)
elif file_exist_01 is False and file_exist_02 is True:
    print('xxx_01.xml 文档不存在')
    sys.exit(1)
elif file_exist_01 is False and file_exist_02 is False:
    print('xxx_01.xml 文档不存在')
    print('xxx_02.xml 文档不存在')
    sys.exit(1)
else:
    diff_xml(xml_path_01, xml_path_02)

print('')
print('-' * 100)
string = input('请按任意键关闭控制台')
if string:
    sys.exit(0)
